﻿/*卡列表--开始*/
//我的宅配,通过Ajax方式获取宅配卡列表信息
function loadCardList(id) {
        var divId = "#tabDiv" + id;
        //if (removeSpaces($(divId).html()) != "") {
        //    $(divId).removeAttr("style").siblings(".my_home").attr("style", "display: none");
        //    var tabId = "#tab" + id;
        //    $(tabId).attr("class", "on").siblings("a").removeAttr("class");
        //    return;
        //}
        var html = "<div>&nbsp;<img src=\"/Content/images/loading.gif\" /> 正在请求数据,请稍候......</div>";
        $(divId).html(html);

        $(divId).removeAttr("style").siblings(".my_home").attr("style", "display: none");
        var tabId = "#tab" + id;
        $(tabId).attr("class", "on").siblings("a").removeAttr("class");

        $.ajax({
            type: "POST",
            url: "/UserHome/MyHomeDeliveryAjax?status=" + id,
            cache: false,
            success: function (data) {
                if (AjaxValidate(data)) {
                    if (data == "") {
                        $(divId).html("您目前还没有宅配套餐");
                    } else {
                        $(divId).html(data);
                    }
                }
                lazyLoad.Run();
            }
        });
}

//点击激活按钮
function activate(cardSysNo, areaGroupSysNo, optionalDays) {
    ShowBack();
    $("#divActivate").show();
    centerDiv("#divActivate");
    bindScroll("#divActivate");
    debugger 
    var days = optionalDays.toString().split(",");
    $("#optionalDays").html("");
    for (var i = 0; i < days.length; i++) {
        var html = "";
        if (i == 0) {
            $("#deliveryDayOfWeek").val(days[i]);
            html = '<li><a href="javascript:" class="on" id="day' + days[i] + '" onclick="selectDayOfWeek(' + days[i] + ')"></a><p>' + dayOfWeek(days[i]) + '</p></li>';
        } else {
            html = '<li><a href="javascript:" id="day' + days[i] + '" onclick="selectDayOfWeek(' + days[i] + ')"></a><p>' + dayOfWeek(days[i]) + '</p></li>';
        }
        $("#optionalDays").append(html);
    }

    $("#areaGroupSysNo").val(areaGroupSysNo);
    $("#addresses").html("");
    $("#goSelectAddress").attr("class", "modify fl");
    $("#cardSysNo").val(cardSysNo);
}

//选择配送日
function selectDayOfWeek(day) { 
    $("#deliveryDayOfWeek").val(day);
    $("#optionalDays li a").each(function(v) {
        $(this).removeAttr("class");
    });
    $("#day" + day).attr("class", "on");
}

//去选择地址
function goSelectAddress() {
    var areaGroupSysNo = $("#areaGroupSysNo").val();

    if ($("#goSelectAddress").attr("class")=="modify on fl"){
        $("#goSelectAddress").attr("class", "modify fl");

        $("#explain").attr("style", "display:block");
        $("#addresses").attr("style", "display:none");
    }
    else if ($("#goSelectAddress").attr("class") == "modify fl") {
        $("#goSelectAddress").attr("class", "modify on fl");
        $("#explain").attr("style", "display:none");
        $("#addresses").attr("style", "display:block");
        
        if (removeSpaces($("#addresses").html()) == "") {
            var html = "<div>&nbsp;<img src=\"/Content/images/loading.gif\" /> 正在请求数据,请稍候......</div>";
            $("#addresses").html(html);
            $.ajax({
                type: "POST",
                url: "/UserHome/Addresses?areaGroupSysNo=" + areaGroupSysNo,
                cache: false,
                success: function (data) {
                    if (AjaxValidate(data)) {
                        if (data == "none" || data.status == -1) {
                            alertShow();
                        } else {
                            $("#addresses").html(data);
                            $("#addressSysNo").val($("#addresses").find("a[class=on]").attr("addressSysNo"));
                        }
                    }
                    lazyLoad.Run();
                }
            });
        }
    }

    centerDiv("#divActivate");
}

//选择地址
function selectAddress(adressSysno) {
    $("#addressSysNo").val(adressSysno);
    $("#address" + adressSysno).attr("class", "on").parent().siblings("li").children("a").removeAttr("class");
}

//点击保存并且激活的时候执行
function saveAndActivate(cardSysNo, deliveryDayOfWeek, addressSysNo) {
    var cardSysNo = $("#cardSysNo").val();
    var deliveryDayOfWeek = $("#deliveryDayOfWeek").val();
    var addressSysNo = $("#addressSysNo").val();

    if (cardSysNo == "" || cardSysNo <= 0) {
        alert("卡号不正确");
        $("#divActivate").hide();
        HideBack();
        initValues();
        unbindScroll();
        return;
    }
    if (deliveryDayOfWeek == "" || deliveryDayOfWeek <= 0) {
        alert("配送日选择不正确");
        $("#divActivate").hide();
        HideBack();
        initValues();
        unbindScroll();
        return;
    }
    if (addressSysNo == "" || addressSysNo <= 0) {
        alert("地址选择不正确");
        $("#divActivate").hide();
        HideBack();
        initValues();
        unbindScroll();
        return;
    }

    $.ajax({
        type: "POST",
        url: "/UserHome/Activate",
        data:{
            sysNo: cardSysNo,
            addressSysNo: addressSysNo,
            type: deliveryDayOfWeek
        },
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    alert("激活成功");
                    $("#divActivate").hide();
                    HideBack();
                    initValues();
                    unbindScroll();
                    $("#liCard"+cardSysNo).remove();
                }
                else {
                    alert("激活失败," + data.msg);
                    $("#divActivate").hide();
                    HideBack();
                    unbindScroll();
                    initValues();
                }
            }
            lazyLoad.Run();
        }
    });
}

//取消激活
function cancelActivate() {
    $("#divActivate").hide();
    initValues()
    HideBack();
    unbindScroll();
}

function navClick(subClassSysNo) {
    $("a[href=#sublist" + subClassSysNo + "]").attr("class", "on").siblings("a").removeAttr("class");
}
/*卡列表--结束*/


/*卡详情--开始*/
//卡详情点击【去修改】按钮的时候执行
function modifyMainDeliveryInfo(cardSysNo, areaGroupSysNo, optionalDays, currentAddress, currentDeliveryDayOfWeek) {

    var needConfirm = false;
    var confirmMsg = "";

    //首先检查子配送信息是否独立设置
    $.ajax({
        type: "POST",
        url: "/UserHome/CheckDeliveryInfo",
        data: {
            sysNo: cardSysNo
        },
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    needConfirm = false;
                }
                else {
                    needConfirm = true;
                    confirmMsg = data.msg;
                }

                if (needConfirm == false || confirm("确认信息:" + confirmMsg) == true) {
                    ShowBack();
                    $("#divModify").show();
                    centerDiv("#divModify");
                    bindScroll("#divModify");

                    var days = optionalDays.toString().split(",");
                    $("#optionalDays").html("");
                    for (var i = 0; i < days.length; i++) {
                        var html = "";
                        if (days[i] == currentDeliveryDayOfWeek) {
                            html = '<li><a href="javascript:" class="on" id="day' + days[i] + '" onclick="selectDayOfWeek(' + days[i] + ')"></a><p>' + dayOfWeek(days[i]) + '</p></li>';
                        } else {
                            html = '<li><a href="javascript:" id="day' + days[i] + '" onclick="selectDayOfWeek(' + days[i] + ')"></a><p>' + dayOfWeek(days[i]) + '</p></li>';
                        }
                        $("#optionalDays").append(html);
                    }

                    $("#areaGroupSysNo").val(areaGroupSysNo);
                    $("#addresses").html("");
                    $("#goModifyAddress").attr("class", "modify");
                    $("#cardSysNo").val(cardSysNo);
                    $("#currentAddress").html(currentAddress);
                    $("#deliveryDayOfWeek").val(currentDeliveryDayOfWeek);
                    $("#flag").val("main");
                }

            }
            lazyLoad.Run();
        }
    });
}

//弹出窗口点击【去修改】按钮的时候执行
function goModifyAddress() {
    var areaGroupSysNo = $("#areaGroupSysNo").val();

    if ($("#goModifyAddress").attr("class") == "modify on") {
        $("#goModifyAddress").attr("class", "modify");
        //关闭地址列表的时候将地址编号置空
        $("#addressSysNo").val("");

        $("#addresses").attr("style", "display:none");
    }
    else if ($("#goModifyAddress").attr("class") == "modify") {
        $("#goModifyAddress").attr("class", "modify on");
        $("#addresses").attr("style", "display:block");

        if (removeSpaces($("#addresses").html()) == "") {
            var html = "<div>&nbsp;<img src=\"/Content/images/loading.gif\" /> 正在请求数据,请稍候......</div>";
            $("#addresses").html(html);
            $.ajax({
                type: "POST",
                url: "/UserHome/Addresses?areaGroupSysNo=" + areaGroupSysNo,
                cache: false,
                success: function (data) {
                    if (AjaxValidate(data)) {
                        if (data == "none" || data.status == -1) {
                            alertShow();
                        } else {
                            $("#addresses").html(data);
                            //点开地址列表的时候设置默认选中的地址编号
                            $("#addressSysNo").val($("#addresses").find("a[class=on]").attr("addressSysNo"));
                        }
                    }
                    lazyLoad.Run();
                }
            });
        }
    }

    centerDiv("#divModify");
}

//配送订单上点击【配送修改】按钮的时候执行
function modifySubDeliveryInfo(soSysNo, areaGroupSysNo, optionalDays, currentAddress, currentDeliveryDayOfWeek) {
    ShowBack();
    $("#divModify").show();
    centerDiv("#divModify");
    bindScroll("#divModify");

    var days = optionalDays.toString().split(",");
    $("#optionalDays").html("");
    for (var i = 0; i < days.length; i++) {
        var html = "";
        if (days[i] == currentDeliveryDayOfWeek) {
            html = '<li><a href="javascript:" class="on" id="day' + days[i] + '" onclick="selectDayOfWeek(' + days[i] + ')"></a><p>' + dayOfWeek(days[i]) + '</p></li>';
        } else {
            html = '<li><a href="javascript:" id="day' + days[i] + '" onclick="selectDayOfWeek(' + days[i] + ')"></a><p>' + dayOfWeek(days[i]) + '</p></li>';
        }
        $("#optionalDays").append(html);
    }

    $("#areaGroupSysNo").val(areaGroupSysNo);
    $("#addresses").html("");
    $("#goModifyAddress").attr("class", "modify");
    $("#soSysNo").val(soSysNo);
    $("#currentAddress").html(currentAddress);
    $("#deliveryDayOfWeek").val(currentDeliveryDayOfWeek);
    $("#flag").val("sub");
}

//关闭修改配送信息窗口
function modifyClose() {
    $("#divModify").hide();
    initValues();
    HideBack();
    unbindScroll();
}

//保存配送信息
function saveDeliveryInfo() {
    var flag = $("#flag").val();
    var cardSysNo = $("#cardSysNo").val();
    var soSysNo = $("#soSysNo").val();
    var deliveryDayOfWeek = $("#deliveryDayOfWeek").val();
    var addressSysNo = $("#addressSysNo").val();

    if (flag == "main") {
        $.ajax({
            type: "POST",
            url: "/UserHome/ModifyMainDeliveryInfo",
            data: {
                sysNo: cardSysNo,
                addressSysNo: addressSysNo,
                type:deliveryDayOfWeek
            },
            cache: false,
            success: function (data) {
                if (AjaxValidate(data)) {
                    if (data.error == "0") {
                        //修改成功
                        alert("修改成功!");
                        window.location.reload();
                    }
                    else {
                        alert("修改失败:"+data.msg);
                        $("#divModify").hide();
                        HideBack();
                        initValues();
                        unbindScroll();
                    }
                }
                lazyLoad.Run();
            }
        });
    } else if (flag == "sub") {
        $.ajax({
            type: "POST",
            url: "/UserHome/ModifySubDeliveryInfo",
            data: {
                sysNo: soSysNo,
                addressSysNo: addressSysNo,
                type: deliveryDayOfWeek
            },
            cache: false,
            success: function (data) {
                if (AjaxValidate(data)) {
                    if (data.error == "0") {
                        alert("修改成功!");
                        window.location.reload();
                    }
                    else {
                        alert("修改失败:" + data.msg);
                        $("#divModify").hide();
                        HideBack();
                        initValues();
                        unbindScroll();
                    }
                }
                lazyLoad.Run();
            }
        });
    }


}

//本周不配
function postpone(soSysNo) {
    if (soSysNo <= 0 || soSysNo == "" || typeof (soSysNo) == undefined) {
        alert("参数错误");
    }

    $.ajax({
        type: "POST",
        url: "/UserHome/Postpone",
        data: {
            sysNo: soSysNo
        },
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    alert("已成功为您延期!");
                    window.location.reload();
                }
                else {
                    alert("延期失败," + data.msg);
                    $("#divModify").hide();
                    HideBack();
                    initValues();
                }
            }
            lazyLoad.Run();
        }
    });
}
/*卡详情--结束*/

/*卡购买页面--开始*/

//数量减去1
function subtract(sysNo) {
    var qty = $("#qty" + sysNo);
    if (parseInt(qty.val()) == NaN) {
        qty.val("1");
    } else {
        var num = parseInt(qty.val()) - 1;
        if (num <= 0) {
            qty.val(1);
        } else {
            qty.val(num);
        }
    }
}

//数量添加1
function add(sysNo) {
    var qty = $("#qty" + sysNo);
    if (parseInt(qty.val()) == NaN) {
        qty.val("1");
    } else {
        var num = parseInt(qty.val()) + 1;
        if (num <= 0) {
            qty.val(1);
        } else {
            qty.val(num);
        }
    }
}

//去结算
function toSettlement(cardSysNo, domain, flag) {
    var qty = $("#qty" + cardSysNo).val();
    if (flag == "productDetailsPage") {
        //如果是宅配卡商品详情页则取详情页中的数量
        qty = $("#textgtSum").val();
    }
    var cardLot = cardSysNo + "," + qty;
    //创建订单
    $.ajax({
        type: "POST",
        url: "/GiftCard/CreateCardOrder?CardLot=" + cardLot,
        cache: false,
        beforeSend: function () {
            Loadding("show");
        },
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.status == -1) {
                    alert("数据加载失败");
                    Loadding("hide");
                    return;
                } else if (data.error != "0") {
                    if (data.error == "100") {
                        window.location.href = data.data;
                    } else {
                        alert("数据加载失败: " + data.message);
                        Loadding("hide");
                        return;
                    }
                } else {
                    //提交订单
                    $.ajax({
                        type: "POST",
                        url: "/GiftCard/SubmitCardOrder",
                        cache: false,
                        success: function (data) {
                            if (AjaxValidate(data)) {
                                if (data.status == -1) {
                                    alert("数据加载失败");
                                    Loadding("hide");
                                    return;
                                } else if (data.error != "0") {
                                    alert("数据加载失败: " + data.message);
                                    Loadding("hide");
                                    return;
                                } else {
                                    var id = data.data.GiftCardOrderSysNo;
                                    var sono = data.data.GiftCardOrderID;
                                    var url = domain + "/Shopping/OnlinePay?id=" + id + "&sono=" + sono + "&osid=0&type=gc";
                                    window.location.href = url;
                                }
                            }
                        }
                    });
                }
            }
        }
    });
}

/*卡购买页面--结束*/

/*其他方法--开始*/
function initValues() {
    $("#cardSysNo").val("");
    $("#deliveryDayOfWeek").val("5");
    $("#addressSysNo").val("");
    $("#areaGroupSysNo").val("");
    if ($("#flag")) {
        $("#flag").val("");
    }

}

function alertShow() {
    centerDiv("#divNoAddress");
    $("#divNoAddress").show();
    $("#divActivate").hide();
    bindScroll("#divNoAddress");
}

function alertClose() {
    $("#divNoAddress").hide();
    HideBack();
    unbindScroll();
}

function ruleShow(rule) {
    $("#pRule").html(rule);
    ShowBack();
    centerDiv("#divRule");
    bindScroll("#divRule");
}

function ruleClose() {
    $("#divRule").hide();
    HideBack();
    unbindScroll();
}

function ruleShow1(rule) {
    $("#pRule").html(rule);
    ShowBack();
    $(".homepc_explain_pop").show();
}

function ruleClose1() {
    $(".homepc_explain_pop").hide();
    HideBack();
}

function centerDiv(divId) {
    var top = ($(window).height() - $(divId).height()) / 2;
    var left = ($(window).width() - $(divId).width()) / 2;
    var scrollTop = $(document).scrollTop();
    var scrollLeft = $(document).scrollLeft();
    $(divId).css({ position: 'absolute', 'top': top + scrollTop, left: left + scrollLeft }).show();
}

function unbindScroll() {
    $(window).unbind('scroll');
}

function bindScroll(divId) {
    unbindScroll();
    $(window).scroll(function () {
        centerDiv(divId);
    });
}

function removeSpaces(inputStr) {
    var outputStr = inputStr.replace(/[\r\n]/g, ""); //去掉回车换行
    outputStr = inputStr.replace(/\s+/g, ""); //去掉空格
    return outputStr;
}

function dayOfWeek(d) {
    if (d == "0") {
        return "周日";
    } else if (d == "1") {
        return "周一";
    } else if (d == "2") {
        return "周二";
    } else if (d == "3") {
        return "周三";
    } else if (d == "4") {
        return "周四";
    } else if (d == "5") {
        return "周五";
    } else if (d == "6") {
        return "周六";
    } else {
        return "未知";
    }
}
/*其他方法--结束*/